<?php
/**
shopexdream.com
 */

class ecstoreappadmincoupon_mdl_ecstoreappadmincoupon extends dbeav_model
{
   #End Func
     /**
     * 中奖的短信等触发器
     * @params string 订单处理动作
     * @params array 订单数据
     * @params int member id
     */
     var $tableName='sdb_ecstoreappadmincoupon_ecstoreappadmincoupon';
    var $defaultOrder = array('id','desc'); //sk

    function getByOrderIdAndItemID($order_id,$orderitem_id){
        return $this->db->select('select * from sdb_ecstoreappadmincoupon_ecstoreappadmincoupon where order_id='.$order_id." and orderitem_id=".$orderitem_id);
    }
    /**
     * 重写getList方法
     * @param string column
     * @param array filter
     * @param int offset
     * @param int limit
     * @param string order by
     */
    public function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null)
    {
        $arr_member = parent::getList($cols, $filter, $offset, $limit, $orderType);
        $members = app::get('b2c')->model('members');

        foreach ($arr_member as $key=>$arr)
        {
            if ($arr['member_id']){
        $rsmem=$members->dump($arr['member_id']);
       // $rsaccount=app::get('pam')->model('members')->dump($arr['member_id']);
/*                $arr_member[$key]['mobile'] = $rsmem['contact']['phone']['mobile'];
        $arr_member[$key]['tel'] =$rsmem['contact']['phone']['telephone'];
        $arr_member[$key]['name'] = $rsmem['contact']['name'];
        $arr_member[$key]['addr'] = $rsmem['contact']['addr'];
        $arr_member[$key]['area'] = $rsmem['contact']['area'];*/
/*        print_r($rsmem);exit;*/

   
      $this->db= kernel::database();
     //  $rsaccount=$this->db->selectRow('select login_name from sdb_pam_account where account_id='.intval($arr['member_id']." and account_type='member'"));
        $rsaccount=$this->db->selectRow('select login_account from sdb_pam_members where member_id='.intval($arr['member_id']));
       //print_r($arr['member_id']);exit;
                $arr_member[$key]['member_id'] =$rsaccount['login_account'];

            }
             if ($arr['buy_member_id']){
   $rsaccount=$this->db->selectRow('select login_account from sdb_pam_members where member_id='.intval($arr['buy_member_id']));
       //print_r($arr['member_id']);exit;
                $arr_member[$key]['buy_member_id'] =$rsaccount['login_account'];//购买人

                }
        }
        return $arr_member;
    }


 //member-luckylist
    public function fetchByMember($member_id, $nPage)
    {
        #$limit = $this->app->getConf("selllog.display.listnum");
        if (!$limit) 
            $limit = 10;
        $limitStart = $nPage * $limit;
        if (!$order_status)
            $filter = array('member_id' => $member_id);
        else
        {
            $filter = array(
                'member_id' => $member_id,
            );
        }
        $sdf_orders = $this->getList('*', $filter, $limitStart, $limit, 'm_activateT DESC');
        
        // 生成分页组建
        $countRd = $this->count($filter);
        $total = ceil($countRd/$limit);
        $current = $nPage;
        $token = '';
        $arrPager = array(
            'current' => $current,
            'total' => $total,
            'token' => $token,
        );
        
/*        $subsdf = array('order_objects'=>array('*',array('order_items'=>array('*',array(':products'=>'*')))));
        foreach ($sdf_orders as &$arr_order)
        {
            $arr_order = $this->dump($arr_order['order_id'], '*', $subsdf);
        }*/
        $arrdata['data'] = $sdf_orders;
        $arrdata['pager'] = $arrPager;
        
        return $arrdata;
     }

     
    public function fireEvent($action , &$object, $member_id=0)
    {
         $trigger = app::get('b2c')->model('trigger');
         $rs= $trigger->object_fire_event($action, $object, $member_id, $this);
         return $rs;
    }
    /*检测订单是否已send card no*/
    function checkexistorder($order_id){
        //
        $sql="select order_id from sdb_ecstoreappadmincoupon_ecstoreappadmincoupon where order_id=".$order_id;
        $rs=$this->db->selectRow($sql);
       // print_r($rs);exit;
        return $rs;
    }
    
    //create card code
    function cardCode($lth){
        //abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
        $chars = '123456789';
        $code = '';
        for($i=0; $i < $lth; $i++)
        {
            $code .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $code;
    }
//8位卡号
    function genCard($codepre,$codeLth="1"){
            $strPre='8';//88888888';//$_POST['codepre'];
            $count =1;// $_POST['count'];
      
            $ret = $this->getLastMNum();
            if($strPre == substr($ret, 0,1))
            {
                $ret = substr($ret, 1);
            }else {
                $ret = "81000000";
            }
                
            $aCard['m_num'] = $strPre;
            $aCard['m_code'] = $this->cardCode($codeLth);
            return $aCard;

    }
    function getLastMNum(){
        $sql = "SELECT m_num FROM sdb_ecstoreappadmincoupon_ecstoreappadmincoupon WHERE id in (SELECT max(id) FROM sdb_ecstoreappadmincoupon_ecstoreappadmincoupon);";
        $ret = $this->db->select($sql);
        //print_r($sql);exit;
        if(count($ret) < 1){
            return "0000000";
        }else{
            $m_num = $ret[0]['m_num'];
            return $m_num;
        }
    }

    function getMCardByMId($member_id){
        $sql = "SELECT m_num, m_code, r_name, m_activateT 
FROM ".$this->tableName.", ".$this->ruleTN."
WHERE ".$this->tableName.".member_id LIKE '".$member_id."'
AND ".$this->tableName.".r_id = ".$this->ruleTN.".r_id;";
        return $this->db->select($sql);
    }

    function updateMCardState($aData){
        $sql = "UPDATE ".$this->tableName." SET m_state = '1', member_id = ".$aData['member_id'].", m_activateT = '".$aData['m_activateT']."' WHERE m_num = '".$aData['m_num']."'";
        //return $this->db->exec($sql);
        return $this->db->exec($sql);
    }

    function getMCardByNum($m_num){
        $sql = "SELECT * 
FROM ".$this->tableName." where m_num='".$m_num."' and m_enabled='0'";
//print_r($sql);exit;
        return $this->db->select($sql);
    }

    function getFieldById($nRId){
        $aSql = "SELECT * FROM ".$this->tableName." WHERE m_id=".intval($nRId)." and disabled='false'";
        return $this->db->select($aSql);
    }
    function saveCard($aData){
        $aSql = "SELECT * FROM ".$this->tableName." WHERE m_id=".intval($aData['m_id']).";";
        $aRs = $this->db->query($aSql);
        $sSql = $this->db->getUpdateSql($aRs,$aData);
        return !$sSql || $this->db->query( $sSql );
    }
    
   
}